import {post, fetch, patch} from '../../../utils/axios'
import router from '../../../router/index'

const state = {
  name: '构建列表',
  search: '',
  totalItems: 0,
  jenkinsDialog: false,
  verifyDialog: false,
  items: [],
  info: '',
  loading: true,
  pagination: {},
  headers: [],
  tempHeader: [],
  fields: {},
  etext: 'No Data',
  rowsPerPageItems: [10, 50, 100, { text: 'All', value: 100000 }],
  searchLabel: 'bagName',
  interval: '',
  offsetTop: 0,
  verifyTitle: '非特殊情况不建议使用此功能，若要强行使用，请输入验证码',
  bid: '',
  openUpLoadDialog: false,
  submitLoad: false,
  loadTitle: '执行中，请稍后...'
}

const mutations = {
  handlerBuildList (state, payload) {
    state.loading = true
    const { sortBy, descending, page, rowsPerPage } = state.pagination
    let ordering = descending ? '-' + sortBy : sortBy
    fetch('/build/', {
      offset: (page - 1) * rowsPerPage,
      limit: rowsPerPage,
      ordering,
      search: state.search,
      Project: payload
    }).then((response) => {
      state.items = response.results
      state.totalItems = response.count
      state.loading = false
    }).catch((error) => {
      state.etext = error.response.data
    })
  },
  loadBuildHeaders (state) {
    fetch('/getmodels/', {
      appname: 'builds',
      modelname: 'version'
    }).then((response) => {
      state.headers = JSON.parse(response)
      state.tempHeader = JSON.parse(response)
      state.headers.splice(6, 0, {text: 'Actions', align: 'center', sortable: false})
    }).catch((error) => {
      state.etext = error.response
    })
  },
  closeBuildList (state) {
    router.replace({path: '/listPubProject'})
  },
  newBuild (state, payload) {
    state.submitLoad = true
    state.fields.Project = payload
    post('/build/',
      state.fields
    ).then((response) => {
      this.dispatch('listbuild/handlerBuildList', payload)
      state.submitLoad = false
    }).catch((error) => {
      alert(error.response.data)
      state.submitLoad = false
    })
  },
  buildInfo (state, payload) {
    let url = '/' + 'build' + '/' + payload + '/'
    fetch(url, {
    }).then((response) => {
      state.info = response
      document.getElementById('scroll-target').scrollTop = document.getElementById('scroll-target').scrollHeight
    }).catch((error) => {
      state.etext = error.response.data
    })
  },
  forceStop (state) {
    let url = '/' + 'build' + '/' + state.bid + '/'
    patch(url, {
      status: 2
    }).then((response) => {
      alert('已强制停止')
      state.verifyDialog = false
    }).catch((error) => {
      alert(error.response.data)
      state.verifyDialog = false
    })
  }
}

const actions = {
  handlerBuildList ({commit}, payload) {
    if (payload) {
      commit('handlerBuildList', payload)
    }
  },
  loadBuildHeaders ({commit}) {
    commit('loadBuildHeaders')
  },
  closeBuildList ({commit}) {
    commit('closeBuildList')
  },
  newBuild ({commit}, payload) {
    commit('newBuild', payload)
  },
  buildInfo ({commit}, payload) {
    state.jenkinsDialog = true
    state.interval = setInterval(() => {
      commit('buildInfo', payload)
    }, 2000)
  },
  forceStop ({commit}) {
    commit('forceStop')
  },
  stopBuild ({commit}, payload) {
    state.verifyDialog = true
    state.bid = payload
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
